package com.znzz.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.znzz.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class UserDaoTestCase {
    @Autowired
    private UserDao userDao;

    @Test
    void testGetById() {
        userDao.selectById("201910530700");
    }

    @Test
    void save() {
        User user = new User();
        user.setName("李知恩");
        user.setPassword("66666666");
        user.setAddress("韩国");
        user.setSex("女");
        System.out.println(userDao.insert(user));
    }

    @Test
    void testGetAll() {
        userDao.selectList(null);//?
    }

    @Test
    void testDelete() {
        userDao.deleteById("13324325");
    }

    @Test
    void testUpdate() {
        User user = new User();
        user.setName("李知恩");
        user.setPassword("66666666");
        user.setAddress("韩国");
        user.setSex("女");
        user.setId("720191053070");
        userDao.updateById(user);
    }

    @Test
    void getByPages() {
        IPage<User> page = new Page<>(2, 5);
        page = userDao.selectPage(page, null);
        System.out.println(page.getCurrent());
        System.out.println(page.getSize());
        System.out.println(page.getTotal());
        System.out.println(page.getPages());
        System.out.println(page.getRecords());
    }

    /**
     * 按条件查询
     */
    @Test
    void testByCondition() {
        QueryWrapper<User> qw = new QueryWrapper<>();
        IPage<User> page = new Page<>(1, 5);
        qw.like("name", "T");
        userDao.selectPage(page, qw);
    }

    @Test
    void testCondition() {
        String name = "李";
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, User::getName, name);
        userDao.selectList(lqw);
    }
}
